using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Promotions; 
public partial class Admin_CouponEditors_Commerce_Promotions_PercentOffCoupon : System.Web.UI.UserControl, ICouponEditor
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void OnCouponSaved()
    {
        if (CouponSaved != null)
        {
            CouponSaved(this, new EventArgs()); 
        }
    }


    #region ICouponEditor Members


    public event EventHandler<EventArgs> CouponSaved;
    public void NewCoupon(Commerce.Promotions.CouponType cpnType)
    {
        btnGenerate.Visible = true; 
        ViewState["CouponTypeId"] = cpnType.CouponTypeID;
        ViewState["IsNew"] = true; 
    }
    public void EditCoupon(Coupon couponToEdit)
    {
        btnGenerate.Visible = false; 
        Commerce.Promotions.PercentOffCoupon pctCoupon =
            (Commerce.Promotions.PercentOffCoupon)couponToEdit;
        ViewState["CouponTypeId"] = pctCoupon.CouponType.CouponTypeID;
        ViewState["IsNew"] = false;
        this.txtCouponCode.Text = pctCoupon.CouponCode;
        txtCouponCode.ReadOnly = true;
        if (pctCoupon.ExpirationDate.HasValue)
        {
            this.txtExpirationDate.Text = String.Format("{0:d}", pctCoupon.ExpirationDate.Value);
        }
        chkIsSingleUse.Checked = pctCoupon.IsSingleUse;
        lblNumUses.Text = pctCoupon.NumberOfUses.ToString();
        txtDiscount.Text = pctCoupon.PercentOff.ToString(); 
    }
    #endregion
    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        txtCouponCode.Text = Commerce.Promotions.Coupon.GenerateCouponCode(); 
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if ((bool)ViewState["IsNew"])
        {
            PercentOffCoupon pctOffCoupon = new PercentOffCoupon(
                txtCouponCode.Text, CouponType.GetCouponType((int)ViewState["CouponTypeId"])); 
            FillCouponValiues(pctOffCoupon);
        }
        else
        {
            PercentOffCoupon pctOffCoupon =
                (PercentOffCoupon)Coupon.GetCoupon(txtCouponCode.Text);
            FillCouponValiues(pctOffCoupon);
        }
    }

    private void FillCouponValiues(PercentOffCoupon pctOffCoupon)
    {
        if (!String.IsNullOrEmpty(txtExpirationDate.PostedDate))
        {
            pctOffCoupon.ExpirationDate = txtExpirationDate.SelectedDate; 
        }
        else
        {
            pctOffCoupon.ExpirationDate = null;
        }
        pctOffCoupon.IsSingleUse = chkIsSingleUse.Checked;
        pctOffCoupon.PercentOff = int.Parse(txtDiscount.Text);
        pctOffCoupon.SaveCoupon();
        OnCouponSaved();
    }
    protected void btnDelete_Click(object sender, EventArgs e) {
        SubSonic.Query q = new SubSonic.Query("CSK_Coupons");
        q.QueryType = SubSonic.QueryType.Delete;
        q.AddWhere("couponCode", txtCouponCode.Text);
        q.Execute();
        Response.Redirect(Request.Url.PathAndQuery, true);
    }
}
